﻿/// <summary>
/// VisitQualityViewModel constructor.
/// </summary>
function VisitQualityViewModel() {
    var visitVM =
            {
                VisitID: ko.observable()
                , Title: ko.observable()
                , SumUp: ko.observable()
                , Text: ko.observable()
                , Visible: ko.observable()
                , Gps: ko.observable()
                , Pictures: ko.observable()
                , Informations: ko.observable()
                , VisitQuality: ko.observable()

                , Visit: ko.observable(new VisitViewModel())
                , IsSelected: ko.observable(false)
                , IsLoading: ko.observable(false)

                /// <summary>
                /// Load data from an instance of mal.balades.Web.Administration.VisitQuality
                /// </summary>
                , Load: function (visit) {
                    this.VisitQuality(visit);
                    this.VisitID(visit.VisitID);
                    this.Title(visit.Title);
                    this.SumUp(visit.SumUp);
                    this.Text(visit.Text);
                    this.Visible(visit.Visible);
                    this.Gps(visit.Gps);
                    this.Pictures(visit.Pictures);
                    this.Informations(visit.Informations);
                    this.Visit().VisitID(visit.VisitID);
                }

                /// <summary>
                /// Open the visit in the edit panel.
                /// </summary>
                , Open: function () {
                    viewModel.OpenVisit(this);
                }

                /// <summary>
                /// Load VisitQuality datas from the server.
                /// </summary>
                , Select: function () {
                    this.Visit().Load();
                }

                /// <summary>
                /// Convert VisitQualityViewModel object into VisitQuality.
                /// </summary>
                , Unload: function () {
                    var serverVisitQuality = new mal.balades.Business.DataModel.VisitQuality();

                    serverVisitQuality.VisitID = this.VisitID();
                    serverVisitQuality.Title = this.Title();
                    serverVisitQuality.SumUp = this.SumUp();
                    serverVisitQuality.Text = this.Text();
                    serverVisitQuality.Visible = this.Visible();
                    serverVisitQuality.Gps = this.Gps();
                    serverVisitQuality.Pictures = this.Pictures();
                    serverVisitQuality.Informations = this.Informations();

                    return serverVisitQuality;
                }

                /// <summary>
                /// Sends the datas to server in order to be save in database.
                /// </summary>
                , Save: function () {

                    if (this.IsDirty()) {
                        var service = new mal.balades.Web.Administration.VisitService();

                        var visitQuality = this.Unload();
                        var visit = this.Visit().Unload();

                        service.SaveVisit(visitQuality, visit, this.onSaveSuccess, null, this);
                    }
                }

                , onSaveSuccess: function (visitID, userstate) {

                    if (userstate.VisitID() != visitID) {
                        userstate.VisitID(visitID);
                        userstate.Visit().VisitID(visitID);
                    }
                    userstate.Visit().Load(true);
                }
            };

            /// <summary>
            /// Boolean indicating whether the visit has been modified.
            /// </summary>
            visitVM.IsDirty = ko.dependentObservable(function () {

                if (this.VisitQuality() != null) {
                    if (this.Title() != this.VisitQuality().Title ||
                    this.SumUp() != this.VisitQuality().SumUp ||
                    this.Text() != this.VisitQuality().Text ||
                    this.Visible() != this.VisitQuality().Visible ||
                    this.Gps() != this.VisitQuality().Gps ||
                    this.Pictures() != this.VisitQuality().Pictures ||
                    this.Informations() != this.VisitQuality().Informations ||
                    this.Visit().IsDirty()) {
                        return true;
                    }
                }
                return false;
            }, visitVM);

    return visitVM;
}